$(function(){
	$('.toolbar').click(function(){
		$text = $(this).text();
		if ($text == 'Cancel') {
			window.location = "posts.php";
		}
		else {
			//Get post id
			var id = $('#post_id').val();
			var title = $('#title').val();
			var content = $('#markItUp').val();
			var cat_id = $('#categoryname').val();
			var state = $('#state').val();
			//Metadata
			var post_meta = $('#post_meta').val();
			var post_robots = $('#post_robots').val();
			var meta_title = $('#meta_title').val();
			var meta_description = $('#meta_description').val();

			if (window.XMLHttpRequest) {
				xmlhttp = new window.XMLHttpRequest();
			}
			else {
				xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			}
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
					response = xmlhttp.responseText;
					$mess = $('#error-message');
					//Title or content is blank
					if (response == -1) {
						$mess.html('Title or content is required');
						$mess.removeClass('error success').addClass('warning');	
					}
					else {
						//Can't insert or update
						if (response == 0) {
							//Insert
							if (id == '0') { 
								$mess.html('Can not insert the post. Please try again');	
							}
							//Update
							else {
								$mess.html('Can not update the post. Please try again');
							}
							$mess.removeClass('error success').addClass('warning');	
						}
						else {
							//Success
							if (response > 0) {
								if (id == '0') {
									$mess.html('The post is inserted successfull');
									$mess.removeClass('error warning').addClass('success');	
									if ($text == 'Save') {
										window.location = "create-posts.php?id="+response;
									}
									else if ($text == 'Save & Close') {
										window.location = "posts.php";
									}
									//Save and new
									else {
										$('#title').val('');
										$('#markItUp').val('');
										//Metadata
										$('#post_meta').val('');
										$('#post_robots').val('');
										$('#meta_title').val('');
										$('#meta_description').val('');
									}
								}
								//Update
								else {
									$mess.html('The post is updated successfull');
									$mess.removeClass('error warning').addClass('success');
									if ($text == 'Save & Close') {
										window.location = "posts.php";
									}
								}
							}
							//Error
							else {
								$mess.html('Cannot insert the post. Please check the information');
								$mess.removeClass('warning success').addClass('error');
							}	
						}
					}
				}
			}
			//Create paramaters
			parameters = 'id='+id+'&title='+title+'&content='+content+'&cat_id='+cat_id+'&state='+state+'&post_meta='+post_meta+'&post_robots='+post_robots+'&meta_title='+meta_title+'&meta_description='+meta_description;

			xmlhttp.open('POST','scripts/create-post.php',true);
			xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
			xmlhttp.send(parameters);
			return false; //Don't submit form	
		}
		

	});
});